﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sistema_Test_OnLine.Model;
using Sistema_Test_OnLine.Logic;
using System.Security.Cryptography;
using System.Text;

namespace Sistema_Test_OnLine.Presentacion
{
    public partial class Registrarse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static string GetMD5(string str)
        {
            MD5 md5 = MD5CryptoServiceProvider.Create();
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] stream = null;
            StringBuilder sb = new StringBuilder();
            stream = md5.ComputeHash(encoding.GetBytes(str));
            for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
            return sb.ToString();
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            Label2.Text = "";
            Label3.Text = "";
            Label4.Text = "";
            Label5.Text = "";
            Label6.Text = "";

            if (TextBox3.Text == "" || TextBox4.Text == "" || TextBox5.Text == "" || TextBox6.Text == "" || TextBox7.Text == "" || TextBox8.Text == "" || TextBox9.Text == "")
                Label2.Text = " Los campos con * son obligatorios";
            else
            {
                if (!isNumeric(TextBox3.Text))
                    Label3.Text = "Campo Numerico!!";
                else
                {
                    if (TextBox5.Text != TextBox6.Text)
                    {
                        Label5.Text="Contraseña erronea";
                        Label6.Text="Contraseña erronea";
                    }
                    else
                    {
                        string criterio = TextBox3.Text + ",'" + TextBox4.Text + "','" + GetMD5(TextBox6.Text) + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + DropDownList1.Text + "','" + "usuario'";
                        if (existe(TextBox4.Text))
                            Label4.Text = "Usuario ya existe";
                        else
                        {
                            if (Facade.SetUsers(criterio))
                            {
                                Label2.Text = "Registro Exitoso !!!";
                                TextBox3.Text = "";
                                TextBox4.Text = "";
                                TextBox5.Text = "";
                                TextBox6.Text = "";
                                TextBox7.Text = "";
                                TextBox8.Text = "";
                                TextBox9.Text = "";
                            }
                            else Label2.Text = "Error al Registrar";
                        }
                    }
                }
            }
        }

        private bool existe(string p)
        {
            List<User> list =Facade.GetUsers("login = '"+p+"'");
            if (list.Count==0)
                return false;
            else
                return true;
        }

        private bool isNumeric(string p)
        {
            try
            {
                Convert.ToInt32(p);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}